home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / terminal.el.z / terminal.el
Encoding:
Text File  |  1998-10-28  |  44.0 KB  |  1,321 lines

  1. ;;; terminal.el --- terminal emulator for GNU Emacs.
  2.  
  3. ;; Copyright (C) 1986,87,88,89,93,94 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Richard Mlynarik <mly@eddie.mit.edu>
  6. ;; Maintainer: FSF
  7. ;; Keywords: comm, terminals
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;;; This file has been censored by the Communications Decency Act.
  29. ;;; That law was passed under the guise of a ban on pornography, but
  30. ;;; it bans far more than that.  This file did not contain pornography,
  31. ;;; but it was censored nonetheless.
  32.  
  33. ;;; For information on US government censorship of the Internet, and
  34. ;;; what you can do to bring back freedom of the press, see the web
  35. ;;; site http://www.vtw.org/
  36.  
  37. ;;; Code:
  38.  
  39. ;;>>TODO
  40. ;;>> ** Nothing can be done about emacs' meta-lossage **
  41. ;;>>  (without redoing keymaps `sanely' -- ask Mly for details)
  42.  
  43. ;;>> One probably wants to do setenv MORE -c when running with
  44. ;;>>   more-processing enabled.
  45.  
  46. (require 'ehelp)
  47.  
  48. (defvar terminal-escape-char ?\C-^
  49.   "*All characters except for this are passed verbatim through the
  50. terminal-emulator.  This character acts as a prefix for commands
  51. to the emulator program itself.  Type this character twice to send
  52. it through the emulator.  Type ? after typing it for a list of
  53. possible commands.
  54. This variable is local to each terminal-emulator buffer.")
  55.  
  56. (defvar terminal-scrolling t ;;>> Setting this to T sort-of defeats my whole aim in writing this package...
  57.   "*If non-nil, the terminal-emulator will losingly `scroll' when output occurs
  58. past the bottom of the screen.  If nil, output will win and `wrap' to the top
  59. of the screen.
  60. This variable is local to each terminal-emulator buffer.")
  61.  
  62. (defvar terminal-more-processing t
  63.   "*If non-nil, do more-processing.
  64. This variable is local to each terminal-emulator buffer.")
  65.  
  66. ;; If you are the sort of loser who uses scrolling without more breaks
  67. ;; and expects to actually see anything, you should probably set this to
  68. ;; around 400
  69. (defvar terminal-redisplay-interval 5000
  70.   "*Maximum number of characters which will be processed by the
  71. terminal-emulator before a screen redisplay is forced.
  72. Set this to a large value for greater throughput,
  73. set it smaller for more frequent updates but overall slower
  74. performance.")
  75.  
  76. (defvar terminal-more-break-insertion
  77.   "*** More break -- Press space to continue ***")
  78.  
  79. (defvar terminal-meta-map nil)
  80. (if terminal-meta-map
  81.     nil
  82.   (let ((map (make-sparse-keymap)))
  83.     (define-key map [t] 'te-pass-through)
  84.     (setq terminal-meta-map map)))
  85.  
  86. (defvar terminal-map nil)
  87. (if terminal-map
  88.     nil
  89.   (let ((map (make-sparse-keymap)))
  90.     (define-key map [t] 'te-pass-through)
  91.     (define-key map [switch-frame] 'handle-switch-frame)
  92.     (define-key map "\e" terminal-meta-map)
  93.     ;(define-key map "\C-l"
  94.     ;  '(lambda () (interactive) (te-pass-through) (redraw-display)))
  95.     (setq terminal-map map)))
  96.  
  97. (defvar terminal-escape-map nil)
  98. (if terminal-escape-map
  99.     nil
  100.   (let ((map (make-sparse-keymap)))
  101.     (define-key map [t] 'undefined)
  102.     (let ((s "0"))
  103.       (while (<= (aref s 0) ?9)
  104.     (define-key map s 'digit-argument)
  105.     (aset s 0 (1+ (aref s 0)))))
  106.     (define-key map "b" 'switch-to-buffer)
  107.     (define-key map "o" 'other-window)
  108.     (define-key map "e" 'te-set-escape-char)
  109.     (define-key map "\C-l" 'redraw-display)
  110.     (define-key map "\C-o" 'te-flush-pending-output)
  111.     (define-key map "m" 'te-toggle-more-processing)
  112.     (define-key map "x" 'te-escape-extended-command)
  113.     ;;>> What use is this?  Why is it in the default terminal-emulator map?
  114.     (define-key map "w" 'te-edit)
  115.     (define-key map "?" 'te-escape-help)
  116.     (define-key map (char-to-string help-char) 'te-escape-help)
  117.     (setq terminal-escape-map map)))
  118.  
  119. (defvar te-escape-command-alist nil)
  120. (if te-escape-command-alist
  121.     nil
  122.   (setq te-escape-command-alist
  123.     '(("Set Escape Character" . te-set-escape-char)
  124.           ;;>> What use is this?  Why is it in the default terminal-emulator map?
  125.       ("Edit" . te-edit)
  126.       ("Refresh" . redraw-display)
  127.       ("Record Output" . te-set-output-log)
  128.       ("Photo" . te-set-output-log)
  129.       ("Tofu" . te-tofu) ;; confuse the uninitiated
  130.       ("Stuff Input" . te-stuff-string)
  131.       ("Flush Pending Output" . te-flush-pending-output)
  132.       ("Enable More Processing" . te-enable-more-processing)
  133.       ("Disable More Processing" . te-disable-more-processing)
  134.       ("Scroll at end of page" . te-do-scrolling)
  135.       ("Wrap at end of page" . te-do-wrapping)
  136.       ("Switch To Buffer" . switch-to-buffer)
  137.       ("Other Window" . other-window)
  138.       ("Kill Buffer" . kill-buffer)
  139.       ("Help" . te-escape-help)
  140.       ("Set Redisplay Interval" . te-set-redisplay-interval)
  141.       )))
  142.  
  143. (defvar terminal-more-break-map nil)
  144. (if terminal-more-break-map
  145.     nil
  146.   (let ((map (make-sparse-keymap)))
  147.     (define-key map [t] 'te-more-break-unread)
  148.     (define-key map (char-to-string help-char) 'te-more-break-help)
  149.     (define-key map " " 'te-more-break-resume)
  150.     (define-key map "\C-l" 'redraw-display)
  151.     (define-key map "\C-o" 'te-more-break-flush-pending-output)
  152.     ;;>>> this isn't right
  153.     ;(define-key map "\^?" 'te-more-break-flush-pending-output) ;DEL
  154.     (define-key map "\r" 'te-more-break-advance-one-line)
  155.  
  156.     (setq terminal-more-break-map map)))
  157.  
  158.  
  159. ;;; Pacify the byte compiler
  160. (defvar te-process nil)
  161. (defvar te-log-buffer nil)
  162. (defvar te-height nil)
  163. (defvar te-width nil)
  164. (defvar te-more-count nil)
  165. (defvar te-redisplay-count nil)
  166. (defvar te-pending-output nil)
  167. (defvar te-saved-point)
  168. (defvar te-more-old-point nil)
  169. (defvar te-more-old-local-map nil)
  170. (defvar te-more-old-filter nil)
  171. (defvar te-more-old-mode-line-format nil)
  172. (defvar te-pending-output-info nil)
  173.  
  174. ;; Required to support terminfo systems
  175. (defconst te-terminal-name-prefix "emacs-em"
  176.   "Prefix used for terminal type names for Terminfo.")
  177. (defconst te-terminfo-directory "/tmp/emacs-terminfo/"
  178.   "Directory used for run-time terminal definition files for Terminfo.")
  179. (defvar te-terminal-name nil)
  180.  
  181. ;;;;  escape map
  182.  
  183. (defun te-escape ()
  184.   (interactive)
  185.   (let (s
  186.         (local (current-local-map))
  187.         (global (current-global-map)))
  188.     (unwind-protect
  189.         (progn
  190.           (use-global-map terminal-escape-map)
  191.           (use-local-map terminal-escape-map)
  192.           (setq s (read-key-sequence
  193.                     (if current-prefix-arg
  194.                         (format "Emacs Terminal escape> %d "
  195.                                 (prefix-numeric-value current-prefix-arg))
  196.                         "Emacs Terminal escape> "))))
  197.       (use-global-map global)
  198.       (use-local-map local))
  199.  
  200.     (message "")
  201.  
  202.     (cond
  203.      ;;  Certain keys give vector notation, like [escape] when
  204.      ;;  you hit esc key...
  205.      ((and (stringp s)
  206.        (string= s (make-string 1 terminal-escape-char)))
  207.       (setq last-command-char terminal-escape-char)
  208.       (let ((terminal-escape-char -259))
  209.     (te-pass-through)))
  210.  
  211.      ((setq s (lookup-key terminal-escape-map s))
  212.       (call-interactively s)))
  213.  
  214.     ))
  215.  
  216.  
  217. (defun te-escape-help ()
  218.   "Provide help on commands available after terminal-escape-char is typed."
  219.   (interactive)
  220.   (message "Terminal emulator escape help...")
  221.   (let ((char (single-key-description terminal-escape-char)))
  222.     (with-electric-help
  223.       (function (lambda ()
  224.      (princ (format "Terminal-emulator escape, invoked by \"%s\"
  225. Type \"%s\" twice to send a single \"%s\" through.
  226.  
  227. Other chars following \"%s\" are interpreted as follows:\n"
  228.             char char char char))
  229.  
  230.      (princ (substitute-command-keys "\\{terminal-escape-map}\n"))
  231.      (princ (format "\nSubcommands of \"%s\" (%s)\n"
  232.             (where-is-internal 'te-escape-extended-command
  233.                        terminal-escape-map t)
  234.             'te-escape-extended-command))
  235.      (let ((l (if (fboundp 'sortcar)
  236.               (sortcar (copy-sequence te-escape-command-alist)
  237.                    'string<)
  238.               (sort (copy-sequence te-escape-command-alist)
  239.                 (function (lambda (a b)
  240.                               (string< (car a) (car b))))))))
  241.        (while l
  242.          (let ((doc (or (documentation (cdr (car l)))
  243.                 "Not documented")))
  244.            (if (string-match "\n" doc)
  245.            ;; just use first line of documentation
  246.            (setq doc (substring doc 0 (match-beginning 0))))
  247.            (princ "  \"")
  248.            (princ (car (car l)))
  249.            (princ "\":\n     ")
  250.            (princ doc)
  251.            (write-char ?\n))
  252.          (setq l (cdr l))))
  253.      nil)))))
  254.  
  255.  
  256.  
  257. (defun te-escape-extended-command ()
  258.   (interactive)
  259.   (let ((c (let ((completion-ignore-case t))
  260.          (completing-read "terminal command: "
  261.                   te-escape-command-alist
  262.                   nil t))))
  263.     (if c
  264.     (catch 'foo
  265.       (setq c (downcase c))
  266.       (let ((l te-escape-command-alist))
  267.         (while l
  268.           (if (string= c (downcase (car (car l))))
  269.           (throw 'foo (call-interactively (cdr (car l))))
  270.         (setq l (cdr l)))))))))
  271.  
  272. ;; not used.
  273. (defun te-escape-extended-command-unread ()
  274.   (interactive)
  275.   (setq unread-command-events (listify-key-sequence (this-command-keys)))
  276.   (te-escape-extended-command))
  277.  
  278. (defun te-set-escape-char (c)
  279.   "Change the terminal-emulator escape character."
  280.   (interactive "cSet escape character to: ")
  281.   (let ((o terminal-escape-char))
  282.     (message (if (= o c)
  283.          "\"%s\" is the escape char"
  284.              "\"%s\" is now the escape; \"%s\" passes through")
  285.          (single-key-description c)
  286.          (single-key-description o))
  287.     (setq terminal-escape-char c)))
  288.  
  289.  
  290. (defun te-stuff-string (string)
  291.   "Read a string to send to through the terminal emulator
  292. as though that string had been typed on the keyboard.
  293.  
  294. Very poor man's file transfer protocol."
  295.   (interactive "sStuff string: ")
  296.   (process-send-string te-process string))
  297.  
  298. (defun te-set-output-log (name)
  299.   "Record output from the terminal emulator in a buffer."
  300.   (interactive (list (if te-log-buffer
  301.              nil
  302.                (read-buffer "Record output in buffer: "
  303.                     (format "%s output-log"
  304.                         (buffer-name (current-buffer)))
  305.                     nil))))
  306.   (if (or (null name) (equal name ""))
  307.       (progn (setq te-log-buffer nil)
  308.          (message "Output logging off."))
  309.     (if (get-buffer name)
  310.     nil
  311.       (save-excursion
  312.     (set-buffer (get-buffer-create name))
  313.     (fundamental-mode)
  314.     (buffer-disable-undo (current-buffer))
  315.     (erase-buffer)))
  316.     (setq te-log-buffer (get-buffer name))
  317.     (message "Recording terminal emulator output into buffer \"%s\""
  318.          (buffer-name te-log-buffer))))
  319.  
  320. (defun te-tofu ()
  321.   "Discontinue output log."
  322.   (interactive)
  323.   (te-set-output-log nil))
  324.  
  325.  
  326. (defun te-toggle (sym arg)
  327.   (set sym (cond ((not (numberp arg)) arg)
  328.          ((= arg 1) (not (symbol-value sym)))
  329.          ((< arg 0) nil)
  330.          (t t))))
  331.  
  332. (defun te-toggle-more-processing (arg)
  333.   (interactive "p")
  334.   (message (if (te-toggle 'terminal-more-processing arg)
  335.            "More processing on" "More processing off"))
  336.   (if terminal-more-processing (setq te-more-count -1)))
  337.  
  338. (defun te-toggle-scrolling (arg)
  339.   (interactive "p")
  340.   (message (if (te-toggle 'terminal-scrolling arg)
  341.            "Scroll at end of page" "Wrap at end of page")))
  342.  
  343. (defun te-enable-more-processing ()
  344.   "Enable ** MORE ** processing"
  345.   (interactive)
  346.   (te-toggle-more-processing t))
  347.  
  348. (defun te-disable-more-processing ()
  349.   "Disable ** MORE ** processing"
  350.   (interactive)
  351.   (te-toggle-more-processing nil))
  352.  
  353. (defun te-do-scrolling ()
  354.   "Scroll at end of page (yuck)"
  355.   (interactive)
  356.   (te-toggle-scrolling t))
  357.  
  358. (defun te-do-wrapping ()
  359.   "Wrap to top of window at end of page"
  360.   (interactive)
  361.   (te-toggle-scrolling nil))
  362.  
  363.  
  364. (defun te-set-redisplay-interval (arg)
  365.   "Set the maximum interval (in output characters) between screen updates.
  366. Set this number to large value for greater throughput,
  367. set it smaller for more frequent updates (but overall slower performance."
  368.   (interactive "NMax number of output chars between redisplay updates: ")
  369.   (setq arg (max arg 1))
  370.   (setq terminal-redisplay-interval arg
  371.     te-redisplay-count 0))
  372.  
  373. ;;;; more map
  374.  
  375. ;; every command -must- call te-more-break-unwind
  376. ;; or grave lossage will result
  377.  
  378. (put 'te-more-break-unread 'suppress-keymap t)
  379. (defun te-more-break-unread ()
  380.   (interactive)
  381.   (if (eq last-input-char terminal-escape-char)
  382.       (call-interactively 'te-escape)
  383.     (message "Continuing from more break (\"%s\" typed, %d chars output pending...)"
  384.          (single-key-description last-input-char)
  385.          (te-pending-output-length))
  386.     (setq te-more-count 259259)
  387.     (te-more-break-unwind)
  388.     (let ((terminal-more-processing nil))
  389.       (te-pass-through))))
  390.  
  391. (defun te-more-break-resume ()
  392.   "Proceed past the **MORE** break,
  393. allowing the next page of output to appear"
  394.   (interactive)
  395.   (message "Continuing from more break")
  396.   (te-more-break-unwind))
  397.  
  398. (defun te-more-break-help ()
  399.   "Provide help on commands available in a terminal-emulator **MORE** break"
  400.   (interactive)
  401.   (message "Terminal-emulator more break help...")
  402.   (sit-for 0)
  403.   (with-electric-help
  404.     (function (lambda ()
  405.       (princ "Terminal-emulator more break.\n\n")
  406.       (princ (format "Type \"%s\" (te-more-break-resume)\n%s\n"
  407.              (where-is-internal 'te-more-break-resume
  408.                     terminal-more-break-map t)
  409.              (documentation 'te-more-break-resume)))
  410.       (princ (substitute-command-keys "\\{terminal-more-break-map}\n"))
  411.       (princ "Any other key is passed through to the program
  412. running under the terminal emulator and disables more processing until
  413. all pending output has been dealt with.")
  414.       nil))))
  415.  
  416.  
  417. (defun te-more-break-advance-one-line ()
  418.   "Allow one more line of text to be output before doing another more break."
  419.   (interactive)
  420.   (setq te-more-count 1)
  421.   (te-more-break-unwind))
  422.  
  423. (defun te-more-break-flush-pending-output ()
  424.   "Discard any output which has been received by the terminal emulator but
  425. not yet processed and then proceed from the more break."
  426.   (interactive)
  427.   (te-more-break-unwind)
  428.   (te-flush-pending-output))
  429.  
  430. (defun te-flush-pending-output ()
  431.   "Discard any as-yet-unprocessed output which has been received by
  432. the terminal emulator."
  433.   (interactive)
  434.   ;; this could conceivably be confusing in the presence of
  435.   ;; escape-sequences spanning process-output chunks
  436.   (if (null (cdr te-pending-output))
  437.       (message "(There is no output pending)")
  438.     (let ((length (te-pending-output-length)))
  439.       (message "Flushing %d chars of pending output" length)
  440.       (setq te-pending-output
  441.         (list 0 (format "\n*** %d chars of pending output flushed ***\n"
  442.                 length)))
  443.       (te-update-pending-output-display)
  444.       (te-process-output nil)
  445.       (sit-for 0))))
  446.  
  447.  
  448. (defun te-pass-through ()
  449.   "Character is passed to the program running under the terminal emulator.
  450. One characters is treated specially:
  451. the terminal escape character (normally C-^)
  452. lets you type a terminal emulator command."
  453.   (interactive)
  454.   (cond ((eq last-input-char terminal-escape-char)
  455.      (call-interactively 'te-escape))
  456.     (t
  457.      ;; Convert `return' to C-m, etc.
  458.      (if (and (symbolp last-input-char)
  459.           (get last-input-char 'ascii-character))
  460.          (setq last-input-char (get last-input-char 'ascii-character)))
  461.      ;; Convert meta characters to 8-bit form for transmission.
  462.      (if (and (integerp last-input-char)
  463.           (not (zerop (logand last-input-char ?\M-\^@))))
  464.          (setq last-input-char (+ 128 (logand last-input-char 127))))
  465.      ;; Now ignore all but actual characters.
  466.      ;; (It ought to be possible to send through function
  467.      ;; keys as character sequences if we add a description
  468.      ;; to our termcap entry of what they should look like.)
  469.      (if (integerp last-input-char)
  470.          (progn
  471.            (and terminal-more-processing (null (cdr te-pending-output))
  472.             (te-set-more-count nil))
  473.            (send-string te-process (make-string 1 last-input-char))
  474.            (te-process-output t))
  475.        (message "Function key `%s' ignored"
  476.             (single-key-description last-input-char))))))
  477.  
  478.  
  479. (defun te-set-window-start ()
  480.   (let* ((w (get-buffer-window (current-buffer)))
  481.      (h (if w (window-height w))))
  482.     (cond ((not w)) ; buffer not displayed
  483.       ((>= h (/ (- (point) (point-min)) (1+ te-width)))
  484.        ;; this is the normal case
  485.        (set-window-start w (point-min)))
  486.       ;; this happens if some vandal shrinks our window.
  487.       ((>= h (/ (- (point-max) (point)) (1+ te-width)))
  488.        (set-window-start w (- (point-max) (* h (1+ te-width)) -1)))
  489.       ;; I give up.
  490.       (t nil))))
  491.  
  492. (defun te-pending-output-length ()
  493.   (let ((length (car te-pending-output))
  494.     (tem (cdr te-pending-output)))
  495.     (while tem
  496.       (setq length (+ length (length (car tem))) tem (cdr tem)))
  497.     length))
  498.  
  499. ;;>> What use is this terminal-edit stuff anyway?
  500. ;;>>  If nothing else, it was written by somebody who didn't
  501. ;;>>  competently understand the terminal-emulator...
  502.  
  503. (defvar terminal-edit-map nil)
  504. (if terminal-edit-map
  505.     nil
  506.   (setq terminal-edit-map (make-sparse-keymap))
  507.   (define-key terminal-edit-map "\C-c\C-c" 'terminal-cease-edit))
  508.  
  509. ;; Terminal Edit mode is suitable only for specially formatted data.
  510. (put 'terminal-edit-mode 'mode-class 'special)
  511.  
  512. (defun terminal-edit-mode ()
  513.   "Major mode for editing the contents of a terminal-emulator buffer.
  514. The editing commands are the same as in Fundamental mode,
  515. together with a command \\<terminal-edit-map>to return to terminal emulation: \\[terminal-cease-edit]."
  516.   (use-local-map terminal-edit-map)
  517.   (setq major-mode 'terminal-edit-mode)
  518.   (setq mode-name "Terminal Edit")
  519.   (setq mode-line-modified (default-value 'mode-line-modified))
  520.   (setq mode-line-process nil)
  521.   (run-hooks 'terminal-edit-mode-hook))
  522.  
  523. (defun te-edit ()
  524.   "Start editing the terminal emulator buffer with ordinary Emacs commands."
  525.   (interactive)
  526.   (terminal-edit-mode)
  527.   (force-mode-line-update)
  528.   ;; Make mode line update.
  529.   (if (eq (key-binding "\C-c\C-c") 'terminal-cease-edit)
  530.       (message "Editing: Type C-c C-c to return to Terminal")
  531.     (message "%s"
  532.          (substitute-command-keys
  533.            "Editing: Type \\[terminal-cease-edit] to return to Terminal"))))
  534.  
  535. (defun terminal-cease-edit ()
  536.   "Finish editing message; switch back to Terminal proper."
  537.   (interactive)
  538.  
  539.   ;;>> emulator will blow out if buffer isn't exactly te-width x te-height
  540.   (let ((buffer-read-only nil))
  541.     (widen)
  542.     (let ((opoint (point-marker))
  543.           (width te-width)
  544.           (h (1- te-height)))
  545.       (goto-char (point-min))
  546.       (while (>= h 0)
  547.         (let ((p (point)))
  548.           (cond ((search-forward "\n" (+ p width) 'move)
  549.                  (forward-char -1)
  550.                  (insert-char ?\  (- width (- (point) p)))
  551.                  (forward-char 1))
  552.                 ((eobp)
  553.                  (insert-char ?\  (- width (- (point) p))))
  554.                 ((= (following-char) ?\n)
  555.                  (forward-char 1))
  556.                 (t
  557.                  (setq p (point))
  558.                  (if (search-forward "\n" nil t)
  559.                      (delete-region p (1- (point)))
  560.                      (delete-region p (point-max))))))
  561.         (if (= h 0)
  562.             (if (not (eobp)) (delete-region (point) (point-max)))
  563.             (if (eobp) (insert ?\n)))
  564.         (setq h (1- h)))
  565.       (goto-char opoint)
  566.       (set-marker opoint nil nil)
  567.       (setq te-saved-point (point))
  568.       (setq te-redisplay-count 0)
  569.       (setq te-more-count -1)))
  570.  
  571.   (setq mode-line-modified (default-value 'mode-line-modified))
  572.   (use-local-map terminal-map)
  573.   (setq major-mode 'terminal-mode)
  574.   (setq mode-name "terminal")
  575.   (setq mode-line-process '(":%s")))
  576.  
  577. ;;;; more break hair
  578.  
  579. (defun te-more-break ()
  580.   (te-set-more-count t)
  581.   (make-local-variable 'te-more-old-point)
  582.   (setq te-more-old-point (point))
  583.   (make-local-variable 'te-more-old-local-map)
  584.   (setq te-more-old-local-map (current-local-map))
  585.   (use-local-map terminal-more-break-map)
  586.   (make-local-variable 'te-more-old-filter)
  587.   (setq te-more-old-filter (process-filter te-process))
  588.   (make-local-variable 'te-more-old-mode-line-format)
  589.   (setq te-more-old-mode-line-format mode-line-format
  590.     mode-line-format (list "--   **MORE**  "
  591.                    mode-line-buffer-identification
  592.                    "%-"))
  593.   (set-process-filter te-process
  594.     (function (lambda (process string)
  595.         (save-excursion
  596.           (set-buffer (process-buffer process))
  597.           (setq te-pending-output (nconc te-pending-output
  598.                          (list string))))
  599.           (te-update-pending-output-display))))
  600.   (te-update-pending-output-display)
  601.   (if (eq (window-buffer (selected-window)) (current-buffer))
  602.       (message "More break "))
  603.   (or (eobp)
  604.       (null terminal-more-break-insertion)
  605.       (save-excursion
  606.     (forward-char 1)
  607.     (delete-region (point) (+ (point) te-width))
  608.     (insert terminal-more-break-insertion)))
  609.   (run-hooks 'terminal-more-break-hook)
  610.   (sit-for 0) ;get display to update
  611.   (throw 'te-process-output t))
  612.  
  613. (defun te-more-break-unwind ()
  614.   (use-local-map te-more-old-local-map)
  615.   (set-process-filter te-process te-more-old-filter)
  616.   (goto-char te-more-old-point)
  617.   (setq mode-line-format te-more-old-mode-line-format)
  618.   (force-mode-line-update)
  619.   (let ((buffer-read-only nil))
  620.     (cond ((eobp))
  621.       (terminal-more-break-insertion
  622.        (forward-char 1)
  623.        (delete-region (point)
  624.               (+ (point) (length terminal-more-break-insertion)))
  625.        (insert-char ?\  te-width)
  626.        (goto-char te-more-old-point)))
  627.     (setq te-more-old-point nil)
  628.     (let ((te-more-count 259259))
  629.       (te-newline)))
  630.   ;(sit-for 0)
  631.   (te-process-output t))
  632.  
  633. (defun te-set-more-count (newline)
  634.   (let ((line (/ (- (point) (point-min)) (1+ te-width))))
  635.     (if newline (setq line (1+ line)))
  636.     (cond ((= line te-height)
  637.        (setq te-more-count te-height))
  638.       ;>>>> something is strange.  Investigate this!
  639.       ((= line (1- te-height))
  640.        (setq te-more-count te-height))
  641.       ((or (< line (/ te-height 2))
  642.            (> (- te-height line) 10))
  643.        ;; break at end of this page
  644.        (setq te-more-count (- te-height line)))
  645.       (t
  646.        ;; migrate back towards top (ie bottom) of screen.
  647.        (setq te-more-count (- te-height
  648.                   (if (> te-height 10) 2 1)))))))
  649.  
  650.  
  651. ;;;; More or less straight-forward terminal escapes
  652.  
  653. ;; ^j, meaning `newline' to non-display programs.
  654. ;; (Who would think of ever writing a system which doesn't understand
  655. ;;  display terminals natively?  Un*x:  The Operating System of the Future.)
  656. (defun te-newline ()
  657.   "Move down a line, optionally do more processing, perhaps wrap/scroll,
  658. move to start of new line, clear to end of line."
  659.   (end-of-line)
  660.   (cond ((not terminal-more-processing))
  661.     ((< (setq te-more-count (1- te-more-count)) 0)
  662.      (te-set-more-count t))
  663.     ((eql te-more-count 0)
  664.      ;; this doesn't return
  665.      (te-more-break)))
  666.   (if (eobp)
  667.       (progn
  668.     (delete-region (point-min) (+ (point-min) te-width))
  669.     (goto-char (point-min))
  670.     (if terminal-scrolling
  671.         (progn (delete-char 1)
  672.            (goto-char (point-max))
  673.            (insert ?\n))))
  674.     (forward-char 1)
  675.     (delete-region (point) (+ (point) te-width)))
  676.   (insert-char ?\  te-width)
  677.   (beginning-of-line)
  678.   (te-set-window-start))
  679.  
  680. ; ^p = x+32 y+32
  681. (defun te-move-to-position ()
  682.   ;; must offset by #o40 since cretinous unix won't send a 004 char through
  683.   (let ((y (- (te-get-char) 32))
  684.     (x (- (te-get-char) 32)))
  685.     (if (or (> x te-width)
  686.         (> y te-height))
  687.     ()
  688.       (goto-char (+ (point-min) x (* y (1+ te-width))))
  689.       ;(te-set-window-start?)
  690.       ))
  691.   (setq te-more-count -1))
  692.  
  693.  
  694.  
  695. ;; ^p c
  696. (defun te-clear-rest-of-line ()
  697.   (save-excursion
  698.     (let ((n (- (point) (progn (end-of-line) (point)))))
  699.       (delete-region (point) (+ (point) n))
  700.       (insert-char ?\  (- n)))))
  701.  
  702.  
  703. ;; ^p C
  704. (defun te-clear-rest-of-screen ()
  705.   (save-excursion
  706.     (te-clear-rest-of-line)
  707.     (while (progn (end-of-line) (not (eobp)))
  708.       (forward-char 1) (end-of-line)
  709.       (delete-region (- (point) te-width) (point))
  710.       (insert-char ?\  te-width))))
  711.  
  712.  
  713. ;; ^p ^l
  714. (defun te-clear-screen ()
  715.   ;; regenerate buffer to compensate for (nonexistent!!) bugs.
  716.   (erase-buffer)
  717.   (let ((i 0))
  718.     (while (< i te-height)
  719.       (setq i (1+ i))
  720.       (insert-char ?\  te-width)
  721.       (insert ?\n)))
  722.   (delete-region (1- (point-max)) (point-max))
  723.   (goto-char (point-min))
  724.   (setq te-more-count -1))
  725.  
  726.  
  727. ;; ^p ^o count+32
  728. (defun te-insert-lines ()
  729.   (if (not (bolp))
  730.       ();(error "fooI")
  731.     (save-excursion
  732.       (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
  733.          (n (min (- (te-get-char) ?\ ) line))
  734.          (i 0))
  735.     (delete-region (- (point-max) (* n (1+ te-width))) (point-max))
  736.     (if (eql (point) (point-max)) (insert ?\n))
  737.     (while (< i n)
  738.       (setq i (1+ i))
  739.       (insert-char ?\  te-width)
  740.       (or (eql i line) (insert ?\n))))))
  741.   (setq te-more-count -1))
  742.  
  743.  
  744. ;; ^p ^k count+32
  745. (defun te-delete-lines ()
  746.   (if (not (bolp))
  747.       ();(error "fooD")
  748.     (let* ((line (- te-height (/ (- (point) (point-min)) (1+ te-width)) -1))
  749.        (n (min (- (te-get-char) ?\ ) line))
  750.        (i 0))
  751.       (delete-region (point)
  752.              (min (+ (point) (* n (1+ te-width))) (point-max)))
  753.       (save-excursion
  754.     (goto-char (point-max))
  755.     (while (< i n)
  756.       (setq i (1+ i))
  757.       (insert-char ?\  te-width)
  758.       (or (eql i line) (insert ?\n))))))
  759.   (setq te-more-count -1))
  760.  
  761. ;; ^p ^a
  762. (defun te-beginning-of-line ()
  763.   (beginning-of-line))
  764.  
  765. ;; ^p ^b
  766. (defun te-backward-char ()
  767.   (if (not (bolp))
  768.       (backward-char 1)))
  769.  
  770. ;; ^p ^f
  771. (defun te-forward-char ()
  772.   (if (not (eolp))
  773.       (forward-char 1)))
  774.  
  775.  
  776. ;; 0177
  777. (defun te-delete ()
  778.   (if (bolp)
  779.       ()
  780.     (delete-region (1- (point)) (point))
  781.     (insert ?\ )
  782.     (forward-char -1)))
  783.  
  784. ;; ^p ^g
  785. (defun te-beep ()
  786.   (beep))
  787.  
  788.  
  789. ;; ^p _ count+32
  790. (defun te-insert-spaces ()
  791.   (let* ((p (point))
  792.      (n (min (- (te-get-char) 32)
  793.          (- (progn (end-of-line) (point)) p))))
  794.     (if (<= n 0)
  795.     nil
  796.       (delete-char (- n))
  797.       (goto-char p)
  798.       (insert-char ?\  n))
  799.     (goto-char p)))
  800.  
  801. ;; ^p d count+32  (should be ^p ^d but cretinous un*x won't send ^d chars!!!)
  802. (defun te-delete-char ()
  803.   (let* ((p (point))
  804.      (n (min (- (te-get-char) 32)
  805.          (- (progn (end-of-line) (point)) p))))
  806.     (if (<= n 0)
  807.     nil
  808.       (insert-char ?\  n)
  809.       (goto-char p)
  810.       (delete-char n))
  811.     (goto-char p)))
  812.  
  813.  
  814.  
  815. ;; disgusting unix-required excrement
  816. ;;  Are we living twenty years in the past yet?
  817.  
  818. (defun te-losing-unix ()
  819.   nil)
  820.  
  821. ;; ^i
  822. (defun te-output-tab ()
  823.   (let* ((p (point))
  824.      (x (- p (progn (beginning-of-line) (point))))
  825.      (l (min (- 8 (logand x 7))
  826.          (progn (end-of-line) (- (point) p)))))
  827.     (goto-char (+ p l))))
  828.  
  829. ;; ^p ^j
  830. ;; Handle the `do' or `nl' termcap capability.
  831. ;;>> I am not sure why this broken, obsolete, capability is here.
  832. ;;>> Perhaps it is for VIle.  No comment was made about why it
  833. ;;>> was added (in "Sun Dec  6 01:22:27 1987  Richard Stallman")
  834. (defun te-down-vertically-or-scroll ()
  835.   "Move down a line vertically, or scroll at bottom."
  836.   (let ((column (current-column)))
  837.     (end-of-line)
  838.     (if (eobp)
  839.     (progn
  840.       (delete-region (point-min) (+ (point-min) te-width))
  841.       (goto-char (point-min))
  842.       (delete-char 1)
  843.       (goto-char (point-max))
  844.       (insert ?\n)
  845.       (insert-char ?\  te-width)
  846.       (beginning-of-line))
  847.       (forward-line 1))
  848.     (move-to-column column))
  849.   (te-set-window-start))
  850.  
  851. ;; Also:
  852. ;;  ^m => beginning-of-line (for which it -should- be using ^p ^a, right?!!)
  853. ;;  ^g => te-beep (for which it should use ^p ^g)
  854. ;;  ^h => te-backward-char (for which it should use ^p ^b)
  855.  
  856.  
  857.  
  858. (defun te-filter (process string)
  859.   (let* ((obuf (current-buffer)))
  860.     ;; can't use save-excursion, as that preserves point, which we don't want
  861.     (unwind-protect
  862.     (progn
  863.       (set-buffer (process-buffer process))
  864.       (goto-char te-saved-point)
  865.       (and (bufferp te-log-buffer)
  866.            (if (null (buffer-name te-log-buffer))
  867.            ;; killed
  868.            (setq te-log-buffer nil)
  869.          (set-buffer te-log-buffer)
  870.          (goto-char (point-max))
  871.          (insert-before-markers string)
  872.          (set-buffer (process-buffer process))))
  873.       (setq te-pending-output (nconc te-pending-output (list string)))
  874.       (te-update-pending-output-display)
  875.       (te-process-output (eq (current-buffer)
  876.                  (window-buffer (selected-window))))
  877.       (set-buffer (process-buffer process))
  878.       (setq te-saved-point (point)))
  879.       (set-buffer obuf))))
  880.  
  881. ;; (A version of the following comment which might be distractingly offensive
  882. ;; to some readers has been moved to term-nasty.el.)
  883. ;; unix lacks ITS-style tty control...
  884. (defun te-process-output (preemptible)
  885.   ;;>> There seems no good reason to ever disallow preemption
  886.   (setq preemptible t)
  887.   (catch 'te-process-output
  888.     (let ((buffer-read-only nil)
  889.       (string nil) ostring start char (matchpos nil))
  890.       (while (cdr te-pending-output)
  891.     (setq ostring string
  892.           start (car te-pending-output)
  893.           string (car (cdr te-pending-output))
  894.           char (aref string start))
  895.     (if (eql (setq start (1+ start)) (length string))
  896.         (progn (setq te-pending-output
  897.                (cons 0 (cdr (cdr te-pending-output)))
  898.              start 0
  899.              string (car (cdr te-pending-output)))
  900.            (te-update-pending-output-display))
  901.         (setcar te-pending-output start))
  902.     (if (and (> char ?\037) (< char ?\377))
  903.         (cond ((eolp)
  904.            ;; unread char
  905.            (if (eql start 0)
  906.                (setq te-pending-output
  907.                  (cons 0 (cons (make-string 1 char)
  908.                        (cdr te-pending-output))))
  909.                (setcar te-pending-output (1- start)))
  910.            (te-newline))
  911.           ((null string)
  912.            (delete-char 1) (insert char)
  913.            (te-redisplay-if-necessary 1))
  914.           (t
  915.            (let ((end (or (and (eq ostring string) matchpos)
  916.                   (setq matchpos (string-match
  917.                            "[\000-\037\177-\377]"
  918.                            string start))
  919.                   (length string))))
  920.              (delete-char 1) (insert char)
  921.              (setq char (point)) (end-of-line)
  922.              (setq end (min end (+ start (- (point) char))))
  923.              (goto-char char)
  924.              (if (eql end matchpos) (setq matchpos nil))
  925.              (delete-region (point) (+ (point) (- end start)))
  926.              (insert (if (and (eql start 0)
  927.                       (eql end (length string)))
  928.                  string
  929.                      (substring string start end)))
  930.              (if (eql end (length string))
  931.              (setq te-pending-output
  932.                    (cons 0 (cdr (cdr te-pending-output))))
  933.                  (setcar te-pending-output end))
  934.              (te-redisplay-if-necessary (1+ (- end start))))))
  935.       ;; I suppose if I split the guts of this out into a separate
  936.       ;;  function we could trivially emulate different terminals
  937.       ;; Who cares in any case?  (Apart from stupid losers using rlogin)
  938.       (funcall
  939.         (if (eql char ?\^p)
  940.             (or (cdr (assq (te-get-char)
  941.                    '((?= . te-move-to-position)
  942.                  (?c . te-clear-rest-of-line)
  943.                  (?C . te-clear-rest-of-screen)
  944.                  (?\C-o . te-insert-lines)
  945.                  (?\C-k . te-delete-lines)
  946.                  ;; not necessary, but help sometimes.
  947.                  (?\C-a . te-beginning-of-line)
  948.                  (?\C-b . te-backward-char)
  949.                  ;; should be C-d, but un*x
  950.                  ;;  pty's won't send \004 through!
  951.                                  ;; Can you believe this?
  952.                  (?d . te-delete-char)
  953.                  (?_ . te-insert-spaces)
  954.                  ;; random
  955.                  (?\C-f . te-forward-char)
  956.                  (?\C-g . te-beep)
  957.                  (?\C-j . te-down-vertically-or-scroll)
  958.                  (?\C-l . te-clear-screen)
  959.                  )))
  960.             'te-losing-unix)
  961.             (or (cdr (assq char
  962.                    '((?\C-j . te-newline)
  963.                  (?\177 . te-delete)
  964.                  ;; Did I ask to be sent these characters?
  965.                  ;; I don't remember doing so, either.
  966.                  ;; (Perhaps some operating system or
  967.                  ;; other is completely incompetent...)
  968.                  (?\C-m . te-beginning-of-line)
  969.                  (?\C-g . te-beep)
  970.                  (?\C-h . te-backward-char)
  971.                  (?\C-i . te-output-tab))))
  972.             'te-losing-unix)))
  973.       (te-redisplay-if-necessary 1))
  974.     (and preemptible
  975.          (input-pending-p)
  976.          ;; preemptible output!  Oh my!!
  977.          (throw 'te-process-output t)))))
  978.   ;; We must update window-point in every window displaying our buffer
  979.   (let* ((s (selected-window))
  980.      (w s))
  981.     (while (not (eq s (setq w (next-window w))))
  982.       (if (eq (window-buffer w) (current-buffer))
  983.       (set-window-point w (point))))))
  984.  
  985. (defun te-get-char ()
  986.   (if (cdr te-pending-output)
  987.       (let ((start (car te-pending-output))
  988.         (string (car (cdr te-pending-output))))
  989.     (prog1 (aref string start)
  990.       (if (eql (setq start (1+ start)) (length string))
  991.           (setq te-pending-output (cons 0 (cdr (cdr te-pending-output))))
  992.           (setcar te-pending-output start))))
  993.     (catch 'char
  994.       (let ((filter (process-filter te-process)))
  995.     (unwind-protect
  996.         (progn
  997.           (set-process-filter te-process
  998.                   (function (lambda (p s)
  999.                                     (or (eql (length s) 1)
  1000.                                         (setq te-pending-output (list 1 s)))
  1001.                                     (throw 'char (aref s 0)))))
  1002.           (accept-process-output te-process))
  1003.       (set-process-filter te-process filter))))))
  1004.  
  1005.  
  1006. (defun te-redisplay-if-necessary (length)
  1007.   (and (<= (setq te-redisplay-count (- te-redisplay-count length)) 0)
  1008.        (eq (current-buffer) (window-buffer (selected-window)))
  1009.        (waiting-for-user-input-p)
  1010.        (progn (te-update-pending-output-display)
  1011.           (sit-for 0)
  1012.           (setq te-redisplay-count terminal-redisplay-interval))))
  1013.  
  1014. (defun te-update-pending-output-display ()
  1015.   (if (null (cdr te-pending-output))
  1016.       (setq te-pending-output-info "")
  1017.     (let ((length (te-pending-output-length)))
  1018.       (if (< length 1500)
  1019.       (setq te-pending-output-info "")
  1020.     (setq te-pending-output-info (format "(%dK chars output pending) "
  1021.                          (/ (+ length 512) 1024))))))
  1022.   (force-mode-line-update))
  1023.  
  1024.  
  1025. (defun te-sentinel (process message)
  1026.   (cond ((eq (process-status process) 'run))
  1027.     ((null (buffer-name (process-buffer process)))) ;deleted
  1028.     (t (let ((b (current-buffer)))
  1029.          (save-excursion
  1030.            (set-buffer (process-buffer process))
  1031.            (setq buffer-read-only nil)
  1032.            (fundamental-mode)
  1033.            (goto-char (point-max))
  1034.            (delete-blank-lines)
  1035.            (delete-horizontal-space)
  1036.            (insert "\n*******\n" message "*******\n"))
  1037.          (if (and (eq b (process-buffer process))
  1038.               (waiting-for-user-input-p))
  1039.          (progn (goto-char (point-max))
  1040.             (recenter -1)))))))
  1041.  
  1042. (defvar te-stty-string "stty -nl erase '^?' kill '^u' intr '^c' echo pass8"
  1043.   "Shell command to set terminal modes for terminal emulator.")
  1044. ;; This used to have `new' in it, but that loses outside BSD
  1045. ;; and it's apparently not needed in BSD.
  1046.  
  1047. (defvar explicit-shell-file-name nil
  1048.   "*If non-nil, is file name to use for explicitly requested inferior shell.")
  1049.  
  1050. ;;;###autoload
  1051. (defun terminal-emulator (buffer program args &optional width height)
  1052.   "Under a display-terminal emulator in BUFFER, run PROGRAM on arguments ARGS.
  1053. ARGS is a list of argument-strings.  Remaining arguments are WIDTH and HEIGHT.
  1054. BUFFER's contents are made an image of the display generated by that program,
  1055. and any input typed when BUFFER is the current Emacs buffer is sent to that
  1056. program an keyboard input.
  1057.  
  1058. Interactively, BUFFER defaults to \"*terminal*\" and PROGRAM and ARGS
  1059. are parsed from an input-string using your usual shell.
  1060. WIDTH and HEIGHT are determined from the size of the current window
  1061. -- WIDTH will be one less than the window's width, HEIGHT will be its height.
  1062.  
  1063. To switch buffers and leave the emulator, or to give commands
  1064. to the emulator itself (as opposed to the program running under it),
  1065. type Control-^.  The following character is an emulator command.
  1066. Type Control-^ twice to send it to the subprogram.
  1067. This escape character may be changed using the variable `terminal-escape-char'.
  1068.  
  1069. `Meta' characters may not currently be sent through the terminal emulator.
  1070.  
  1071. Here is a list of some of the variables which control the behaviour
  1072. of the emulator -- see their documentation for more information:
  1073. terminal-escape-char, terminal-scrolling, terminal-more-processing,
  1074. terminal-redisplay-interval.
  1075.  
  1076. This function calls the value of terminal-mode-hook if that exists
  1077. and is non-nil after the terminal buffer has been set up and the
  1078. subprocess started."
  1079.   (interactive
  1080.     (cons (save-excursion
  1081.         (set-buffer (get-buffer-create "*terminal*"))
  1082.         (buffer-name (if (or (not (boundp 'te-process))
  1083.                  (null te-process)
  1084.                  (not (eq (process-status te-process)
  1085.                       'run)))
  1086.                  (current-buffer)
  1087.                (generate-new-buffer "*terminal*"))))
  1088.       (append
  1089.         (let* ((default-s
  1090.              ;; Default shell is same thing M-x shell uses.
  1091.              (or explicit-shell-file-name
  1092.              (getenv "ESHELL")
  1093.              (getenv "SHELL")
  1094.              "/bin/sh"))
  1095.            (s (read-string
  1096.                (format "Run program in emulator: (default %s) "
  1097.                    default-s))))
  1098.           (if (equal s "")
  1099.           (list default-s '())
  1100.         (te-parse-program-and-args s))))))
  1101.   (switch-to-buffer buffer)
  1102.   (if (null width) (setq width (- (window-width (selected-window)) 1)))
  1103.   (if (null height) (setq height (- (window-height (selected-window)) 1)))
  1104.   (terminal-mode)
  1105.   (setq te-width width te-height height)
  1106.   (setq te-terminal-name (concat te-terminal-name-prefix te-width
  1107.                  te-height))
  1108.   (setq mode-line-buffer-identification
  1109.     (list (format "Emacs terminal %dx%d: %%b  " te-width te-height)
  1110.           'te-pending-output-info))
  1111.   (let ((buffer-read-only nil))
  1112.     (te-clear-screen))
  1113.   (let (process)
  1114.     (while (setq process (get-buffer-process (current-buffer)))
  1115.       (if (y-or-n-p (format "Kill process %s? " (process-name process)))
  1116.       (delete-process process)
  1117.     (error "Process %s not killed" (process-name process)))))
  1118.   (condition-case err
  1119.       (let ((process-environment
  1120.          (cons (concat "TERM=" te-terminal-name)
  1121.            (cons (concat "TERMCAP=" (te-create-termcap))
  1122.              (cons (concat "TERMINFO=" (te-create-terminfo))
  1123.                    process-environment)))))
  1124.     (setq te-process
  1125.           (start-process "terminal-emulator" (current-buffer)
  1126.                  "/bin/sh" "-c"
  1127.                  ;; Yuck!!! Start a shell to set some terminal
  1128.                  ;; control characteristics.  Then start the
  1129.                  ;; "env" program to setup the terminal type
  1130.                  ;; Then finally start the program we wanted.
  1131.                  (format "%s; exec %s"
  1132.                      te-stty-string
  1133.                      (mapconcat 'te-quote-arg-for-sh
  1134.                         (cons program args) " "))))
  1135.     (set-process-filter te-process 'te-filter)
  1136.     (set-process-sentinel te-process 'te-sentinel))
  1137.     (error (fundamental-mode)
  1138.        (signal (car err) (cdr err))))
  1139.   (setq inhibit-quit t)            ;sport death
  1140.   (use-local-map terminal-map)
  1141.   (run-hooks 'terminal-mode-hook)
  1142.   (message "Entering emacs terminal-emulator...  Type %s %s for help"
  1143.        (single-key-description terminal-escape-char)
  1144.        (mapconcat 'single-key-description
  1145.               (where-is-internal 'te-escape-help terminal-escape-map t)
  1146.               " ")))
  1147.  
  1148.  
  1149. (defun te-parse-program-and-args (s)
  1150.   (cond ((string-match "\\`\\([-a-zA-Z0-9+=_.@/:]+[ \t]*\\)+\\'" s)
  1151.      (let ((l ()) (p 0))
  1152.        (while p
  1153.          (setq l (cons (if (string-match
  1154.                 "\\([-a-zA-Z0-9+=_.@/:]+\\)\\([ \t]+\\)*"
  1155.                 s p)
  1156.                    (prog1 (substring s p (match-end 1))
  1157.                  (setq p (match-end 0))
  1158.                  (if (eql p (length s)) (setq p nil)))
  1159.                    (prog1 (substring s p)
  1160.                  (setq p nil)))
  1161.                l)))
  1162.        (setq l (nreverse l))
  1163.        (list (car l) (cdr l))))
  1164.     ((and (string-match "[ \t]" s) (not (file-exists-p s)))
  1165.      (list shell-file-name (list "-c" (concat "exec " s))))
  1166.     (t (list s ()))))
  1167.  
  1168. (put 'terminal-mode 'mode-class 'special)
  1169. ;; This is only separated out from function terminal-emulator
  1170. ;; to keep the latter a little more manageable.
  1171. (defun terminal-mode ()
  1172.   "Set up variables for use with the terminal-emulator.
  1173. One should not call this -- it is an internal function
  1174. of the terminal-emulator"
  1175.   (kill-all-local-variables)
  1176.   (buffer-disable-undo (current-buffer))
  1177.   (setq major-mode 'terminal-mode)
  1178.   (setq mode-name "terminal")
  1179. ; (make-local-variable 'Helper-return-blurb)
  1180. ; (setq Helper-return-blurb "return to terminal simulator")
  1181.   (setq mode-line-process '(":%s"))
  1182.   (setq buffer-read-only t)
  1183.   (setq truncate-lines t)
  1184.   (make-local-variable 'terminal-escape-char)
  1185.   (setq terminal-escape-char (default-value 'terminal-escape-char))
  1186.   (make-local-variable 'terminal-scrolling)
  1187.   (setq terminal-scrolling (default-value 'terminal-scrolling))
  1188.   (make-local-variable 'terminal-more-processing)
  1189.   (setq terminal-more-processing (default-value 'terminal-more-processing))
  1190.   (make-local-variable 'terminal-redisplay-interval)
  1191.   (setq terminal-redisplay-interval (default-value 'terminal-redisplay-interval))
  1192.   (make-local-variable 'te-width)
  1193.   (make-local-variable 'te-height)
  1194.   (make-local-variable 'te-process)
  1195.   (make-local-variable 'te-pending-output)
  1196.   (setq te-pending-output (list 0))
  1197.   (make-local-variable 'te-saved-point)
  1198.   (setq te-saved-point (point-min))
  1199.   (make-local-variable 'te-pending-output-info) ;for the mode line
  1200.   (setq te-pending-output-info "")
  1201.   (make-local-variable 'inhibit-quit)
  1202.   ;(setq inhibit-quit t)
  1203.   (make-local-variable 'te-log-buffer)
  1204.   (setq te-log-buffer nil)
  1205.   (make-local-variable 'te-more-count)
  1206.   (setq te-more-count -1)
  1207.   (make-local-variable 'te-redisplay-count)
  1208.   (setq te-redisplay-count terminal-redisplay-interval)
  1209.   ;(use-local-map terminal-mode-map)
  1210.   ;; terminal-mode-hook is called above in function terminal-emulator
  1211.   )
  1212.  
  1213. ;;;; what a complete loss
  1214.  
  1215. (defun te-quote-arg-for-sh (string)
  1216.   (cond ((string-match "\\`[-a-zA-Z0-9+=_.@/:]+\\'"
  1217.                string)
  1218.      string)
  1219.     ((not (string-match "[$]" string))
  1220.      ;; "[\"\\]" are special to sh and the lisp reader in the same way
  1221.      (prin1-to-string string))
  1222.     (t
  1223.      (let ((harder "")
  1224.            (start 0)
  1225.            (end 0))
  1226.        (while (cond ((>= start (length string))
  1227.              nil)
  1228.             ;; this is the set of chars magic with "..." in `sh'
  1229.             ((setq end (string-match "[\"\\$]"
  1230.                          string start))
  1231.              t)
  1232.             (t (setq harder (concat harder
  1233.                         (substring string start)))
  1234.                nil))
  1235.          (setq harder (concat harder (substring string start end)
  1236.                                   ;; Can't use ?\\ since `concat'
  1237.                                   ;; unfortunately does prin1-to-string
  1238.                                   ;; on fixna.  Amazing.
  1239.                   "\\"
  1240.                   (substring string
  1241.                          end
  1242.                          (1+ end)))
  1243.            start (1+ end)))
  1244.        (concat "\"" harder "\"")))))
  1245.  
  1246. (defun te-create-terminfo ()
  1247.   "Create and compile a terminfo entry for the virtual terminal. This is kept
  1248. in the directory specified by `te-terminfo-directory'."
  1249.   (if (and system-uses-terminfo
  1250.        (not (file-exists-p (concat  te-terminfo-directory
  1251.                     (substring te-terminal-name-prefix 0 1)
  1252.                     "/" te-terminal-name))))
  1253.     (let ( (terminfo
  1254.         (concat
  1255.          ;; The first newline avoids trouble with ncurses.
  1256.          (format "%s,\n\tmir, xon,cols#%d, lines#%d,"
  1257.              te-terminal-name te-width te-height)
  1258.          "bel=^P^G, clear=^P\\f, cr=^P^A, cub1=^P^B, cud1=^P\\n,"
  1259.          "cuf1=^P^F, cup=^P=%p1%'\\s'%+%c%p2%'\\s'%+%c,"
  1260.          "dch=^Pd%p1%'\\s'%+%c, dch1=^Pd!, dl=^P^K%p1%'\\s'%+%c,"
  1261.          "dl1=^P^K!, ed=^PC, el=^Pc, home=^P=\\s\\s,"
  1262.          "ich=^P_%p1%'\\s'%+%c, ich1=^P_!, il=^P^O%p1%'\\s'%+%c,"
  1263.          "il1=^P^O!, ind=^P\\n, nel=\\n,\n"))
  1264.        ;; The last newline avoids trouble with ncurses.
  1265.        (file-name (concat te-terminfo-directory te-terminal-name ".tif")) )
  1266.       (make-directory te-terminfo-directory t)
  1267.       (save-excursion
  1268.     (set-buffer (create-file-buffer file-name))
  1269.     (insert terminfo)
  1270.     (write-file file-name)
  1271.     (kill-buffer nil)
  1272.     )
  1273.       (let ( (process-environment
  1274.           (cons (concat "TERMINFO="
  1275.                 (directory-file-name te-terminfo-directory))
  1276.             process-environment)) )
  1277.     (set-process-sentinel (start-process "tic" nil "tic" file-name)
  1278.                   'te-tic-sentinel))))
  1279.     (directory-file-name te-terminfo-directory)
  1280. )
  1281.  
  1282. (defun te-create-termcap ()
  1283.   "Create a termcap entry for the virtual terminal"
  1284.   ;; Because of Unix Brain Death(tm), we can't change
  1285.   ;;  the terminal type of a running process, and so
  1286.   ;;  terminal size and scrollability are wired-down
  1287.   ;;  at this point.  ("Detach?  What's that?")
  1288.   (concat (format "%s:co#%d:li#%d:%s"
  1289.           ;; Sigh.  These can't be dynamically changed.
  1290.           te-terminal-name te-width te-height (if terminal-scrolling
  1291.                      "" "ns:"))
  1292.       ;;-- Basic things
  1293.       ;; cursor-motion, bol, forward/backward char
  1294.       "cm=^p=%+ %+ :cr=^p^a:le=^p^b:nd=^p^f:"
  1295.       ;; newline, clear eof/eof, audible bell
  1296.       "nw=^j:ce=^pc:cd=^pC:cl=^p^l:bl=^p^g:"
  1297.       ;; insert/delete char/line
  1298.       "IC=^p_%+ :DC=^pd%+ :AL=^p^o%+ :DL=^p^k%+ :"
  1299.       ;;-- Not-widely-known (ie nonstandard) flags, which mean
  1300.       ;; o writing in the last column of the last line
  1301.       ;;   doesn't cause idiotic scrolling, and
  1302.       ;; o don't use idiotische c-s/c-q sogenannte
  1303.       ;;   ``flow control'' auf keinen Fall.
  1304.       "LP:NF:"
  1305.       ;;-- For stupid or obsolete programs
  1306.       "ic=^p_!:dc=^pd!:al=^p^o!:dl=^p^k!:ho=^p=  :"
  1307.       ;;-- For disgusting programs.
  1308.       ;; (VI? What losers need these, I wonder?)
  1309.       "im=:ei=:dm=:ed=:mi:do=^p^j:nl=^p^j:bs:")
  1310. )
  1311.  
  1312. (defun te-tic-sentinel (proc state-change)
  1313.   "If tic has finished, delete the .tif file"
  1314.   (if (equal state-change "finished
  1315. ")
  1316.       (delete-file (concat te-terminfo-directory te-terminal-name ".tif"))))
  1317.  
  1318. (provide 'terminal)
  1319.  
  1320. ;;; terminal.el ends here
  1321.